package com.wewoo.log4e.core.Register;

import cn.hutool.core.util.ReflectUtil;
import com.wewoo.log4e.core.LogTemplate;
import com.wewoo.log4e.core.annotation.Log4e;
import com.wewoo.log4e.core.executor.LogExecutor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ClassName ReadTemplate
 * @Description TODO
 * @Author tangjin
 * @Date 11:42 2022/2/22
 **/
@Slf4j
@RequiredArgsConstructor
public class ReadTemplate<T> {
    private final ReadRegister readRegister;
    private final LogTemplate logTemplate;

    public List<T> readLogList(T t) {
        LogExecutor executor = logTemplate.getPrimaryExecutor();
        if (executor.getClass().getName().equals("com.wewoo.log4e.starter.mongodb.executor.MongodbExecutor")) {
            return readRegister.switchRead("ReadLogMongoDBUtil").readLogList(t);
        } else if (executor.getClass().getName().equals("com.wewoo.log4e.starter.executor.DataSourceExecutor")) {
            return readRegister.switchRead("ReadLogDataBaseUtil").readLogList(t);
        } else {
            log.error("The current log primary executor cannot match the corresponding read log tool class");
            return null;
        }
    }

    public List<String> readLogList(){
        LogExecutor executor = logTemplate.getPrimaryExecutor();
        if (executor.getClass().getName().equals("com.wewoo.log4e.starter.mongodb.executor.MongodbExecutor")) {
            return readRegister.switchRead("ReadLogMongoDBUtil").readLogList();
        } else if (executor.getClass().getName().equals("com.wewoo.log4e.starter.executor.DataSourceExecutor")) {
            return readRegister.switchRead("ReadLogDataBaseUtil").readLogList();
        } else {
            log.error("The current log primary executor cannot match the corresponding read log tool class");
            return null;
        }
    }
}
